/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#include "input.h"

#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
//#include <boost/archive/xml_archive_exception.hpp>
// #include <boost/exception.hpp>

#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <exception>

namespace Sirat {

namespace Common {

Input::Input(const char* _file) : file(_file) {
}

Input::Input() : file("/tmp/default.xml") {  }

Input::~Input() { }

void Input::parse() {
	std::ifstream ifs(file);
	assert(ifs.good());
	boost::archive::text_iarchive ia(ifs);
	ia >> BOOST_SERIALIZATION_NVP(rendering_scene);
	rendering_scene.raytracer->set_camera(rendering_scene.camera);
	rendering_scene.raytracer->set_scene(rendering_scene.scene);
	ifs.close();
}

void Input::parse(const char* _file) {
	std::ifstream ifs(_file);
	assert(ifs.good());
	boost::archive::text_iarchive ia(ifs);
	ia >> BOOST_SERIALIZATION_NVP(rendering_scene);
	rendering_scene.raytracer->set_camera(rendering_scene.camera);
	rendering_scene.raytracer->set_scene(rendering_scene.scene);
	ifs.close();
}

void Input::save() {
	std::ofstream ofs(file);
	assert(ofs.good());
	boost::archive::text_oarchive oa(ofs);
	oa << BOOST_SERIALIZATION_NVP(rendering_scene);

	ofs.close();
}

void Input::save(Ray_Tracing::Raytracer* _raytracer,Scene* _scene,Camera* _camera,const char* _file) {
	rendering_scene.raytracer = _raytracer;
	rendering_scene.scene = _scene;
	rendering_scene.camera = _camera;
	std::ofstream ofs(_file);
	assert(ofs.good());
	boost::archive::text_oarchive oa(ofs);
	try {
		oa << BOOST_SERIALIZATION_NVP(rendering_scene);
	}
	catch(boost::archive::archive_exception& e) {
		std::cerr << "Error saving: " << e.what() << std::endl;
	}
	ofs.close();
}

Scene* Input::get_scene() const {
	return rendering_scene.scene;
}

Ray_Tracing::Raytracer* Input::get_raytracer() const {
	return rendering_scene.raytracer;
}

Camera* Input::get_camera() const {
	return rendering_scene.camera;
}

}

}

